//新增费用单

interface IfeeAddToParent extends ng.IScope{
    /**
     * 费用编号
     */
    feeCode:string;
    /**
     * 总费用编号
     */
    feeTotalCode:string;
    /**
     * 承运商名称
     */
    carrier:string;
    /**
     * 承运商号码
     */
    carrierPhone:string;
    /**
     * 总金额
     */
    totalMoney:number;
    /**
     * 持卡人
     */
    holder:string;
    /**
     * 银行卡号
     */
    bankCode:string;
    /**
     * 开户银行
     */
    bankName:string;
    /**
     * 应付说明
     */
    description:string;

    /**
     * 添加至结算单
     */
    addToParentEv():void;
    /**
     * 银行卡是否被修改
     */
    bankIsChange:boolean;
    /**
     * 费用自动填充获取数据
     */
    onFeeSettleBillListAutoComplate(value:string):void;
    /**
     * 总费用编号选中自动填充事件
     */
    onSettleBillSelected(select:angucomplete<FeeSettleBillListItemResponse>):void;

    back:()=>void;
}

class feeAddToParentController{
    /**
     * 费用编号
     */
    feeId:string;
    /**
     * 费用结算总单编号
     */
    feeSettleBillId:string;
    /**
     * 保存原有总额
     */
    totalMoney:number;
    /**
     * 费用方式(枚举)
     */
    paymentMethod:string;
    /**
     * 判断结算方式是否一致
     */
    isSettleType:boolean;
    constructor(private $scope:IfeeAddToParent,private feeService:IFeeService, private $location:ng.ILocationService,private $ngBootbox:BootboxService,private $state:ng.ui.IStateService){
        this.init();
        this.$scope.feeCode = this.$location.search().code;
        
        this.$scope.bankIsChange = false;
        this.isSettleType = true;
    }
    init():void{
        this.feeId = this.$location.search().id;
        this.getFeeDetial();
        this.$scope.onFeeSettleBillListAutoComplate = this.onFeeSettleBillListAutoComplate;
        this.$scope.addToParentEv = this.addToParentEv;
        this.$scope.onSettleBillSelected = this.onSettleBillSelected;

        this.$scope.back= this.back;
    }
    /**
     * 获取费用详情
     */
    getFeeDetial():void{
        this.feeService.feeDetail(this.feeId).then(res=>{
            this.$scope.totalMoney = res.data.feeAmount;
            this.paymentMethod = res.data.paymentMethod;
            this.totalMoney = res.data.feeAmount;
        });
    }
    /**
     * 费用自动填充获取数据
     */
    onFeeSettleBillListAutoComplate=(value:string)=>{
        return this.feeService.getFeeSettleBillList("",true,value,"","","","","","","","","4","","",false,0,5).then(res=>{
            return res.data.data;
        })
    }

    /**
     * 总费用编号选中自动填充
     */
    onSettleBillSelected=(select:angucomplete<FeeSettleBillListItemResponse>)=>{
        if(select){
            this.feeSettleBillId = select.description.id;
            this.feeService.getFeeSettleBillDetail(select.description.id).then(res=>{
                var data = res.data;
                //银行卡是否被修改
                if(res.data.isChangeBank){ this.$scope.bankIsChange = true;}
                this.feeService.getFeeSettleBillList("",true,res.data.billCode,"","","","","","","","","4","","",false,0,5).then(res=>{
                    let paymentMethodStr = res.data.data[0].paymentMethod;
                    if(!((this.paymentMethod=="1")&&(paymentMethodStr=="现金")||(this.paymentMethod=="2")&&(paymentMethodStr=="打卡")||(this.paymentMethod=="3"||this.paymentMethod=="4")&&(paymentMethodStr=="石油油卡"||paymentMethodStr=="石化油卡"))){
                        this.feeSettleBillId = "";
                        this.$scope.carrier = "";
                        this.$scope.carrierPhone = "";
                        this.$scope.totalMoney = this.totalMoney;
                        this.$scope.bankName = "";
                        this.$scope.bankCode = "";
                        this.$scope.holder = "";
                        this.$scope.description = "";
                        // this.$scope.bankIsChange = false;
                        this.$ngBootbox.alert("该结算单与当前费用付款方式不一致");
                    }else{
                        this.feeSettleBillId = select.description.id;
                        this.$scope.carrier = data.carrier;
                        this.$scope.carrierPhone = data.carrierPhone;
                        this.$scope.totalMoney = parseFloat(String(this.totalMoney + data.totalMoney));
                        this.$scope.bankName = data.bankName;
                        this.$scope.bankCode = data.bankCode;
                        this.$scope.holder = data.holder;
                        this.$scope.description = data.description;
                        this.$scope.bankIsChange = data.isChangeBank;
                        this.isSettleType = false;
                    }
                })
            })
        }
    }

    /**
     * 新增费用单
     */
    addToParentEv=()=>{
        if(this.feeSettleBillId !="" && this.feeSettleBillId !=null && this.feeSettleBillId != undefined){
            if(!this.isSettleType){
                this.$ngBootbox.confirm("确定新增结算单吗").then(()=>{
                    this.feeService.addFeeToParent(this.feeId,this.feeSettleBillId).then(res=>{
                        this.$state.go("app.fee.feeCheckList");
                    });
                });
            }else{this.$ngBootbox.alert("该结算单与当前费用付款方式不一致")}
        }else{this.$ngBootbox.alert("请选择正确的结算单")}
    }

    back=()=>{
        this.$state.go("app.fee.feeCheckList",{name:'feeCheckList'})
    }
}

angular.module("tms").controller("feeAddToParentController", ["$scope", "feeService", "$location","$ngBootbox","$state","$state", feeAddToParentController]);